package com.owg.jigsaw.test;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.util.Arrays;

import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import com.owg.jigsaw.WBlock;
import com.owg.jigsaw.WPalette;
import com.owg.jigsaw.WRoot;
import com.owg.jigsaw.WTrashCan;
import com.owg.jigsaw.WWorkArea;
import com.owg.jigsaw.blocktypes.WHPanelBlock;
import com.owg.jigsaw.blocktypes.WLabelBlock;
import com.owg.jigsaw.blocktypes.WVPanelBlock;
import com.owg.jigsaw.blocktypes.WindowBlock;
import com.owg.jigsaw.cutEditor.CutEditor;
import com.owg.jigsaw.hierarchy.TypeHierarchy;
import com.sun.javafx.application.PlatformImpl;


public class WindowBuilder
{	
	public static void main(String[] args)
	{
		PlatformImpl.startup(new Thread());
		Platform.runLater(new Runnable()
		{
			@Override
			public void run()
			{
				WRoot wroot = new WRoot(new TypeHierarchy(Object.class, Container.class, TextField.class, ScrollPane.class, Frame.class, Component.class, Panel.class, Label.class));
				WPalette wpalette = new WPalette(wroot, 
					Arrays.asList(new WBlock[]{
						new WindowBlock(wroot, true), 
						new WHPanelBlock(wroot, true), 
						new WVPanelBlock(wroot, true), 
						new WLabelBlock(wroot, true, "Label")
						}));
				wroot.add(wpalette);
				WWorkArea wworkspace = new WWorkArea(wroot);
				wroot.add(wworkspace);

				WTrashCan wtrashcan = new WTrashCan(wroot);
				wroot.add(wtrashcan);
				
				
				Stage stage = new Stage();
				GridPane ui = new GridPane();
				wroot.installContentPane(ui);
				Pane rootUI = wroot.getUI();
				
				MenuBar menubar = new MenuBar();
				Menu fileMenu = new Menu("File");
				MenuItem runItem = new MenuItem("Run");
				runItem.onActionProperty().set(e -> {mkFrames(wworkspace);});
				fileMenu.getItems().add(runItem );
				
				MenuItem cutEditorItem = new MenuItem("Edit Connectors");
				cutEditorItem.onActionProperty().set(e -> {mkCutEditor(wroot.getTypeHierarchy());});
				fileMenu.getItems().add(cutEditorItem );
				
				MenuItem exitItem = new MenuItem("Quit");
				exitItem.onActionProperty().set(e -> {System.exit(0);});
				fileMenu.getItems().add(exitItem );
				menubar.getMenus().add(fileMenu );
				
				VBox fcp = new VBox();
				VBox.setVgrow(menubar, Priority.NEVER);
				fcp.getChildren().add(menubar);
				VBox.setVgrow(rootUI, Priority.ALWAYS);
				fcp.getChildren().add(rootUI);
				
				Scene scene = new Scene(fcp);
				
				Pane left = wpalette.getUI();
				left.setBackground(new Background(new BackgroundFill(Color.DARKGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
				GridPane.setHgrow(left, Priority.NEVER);
				GridPane.setVgrow(left, Priority.ALWAYS);
				ui.add(left, 0, 0, 1,1);
				
				Pane bot = wtrashcan.getUI();
				GridPane.setHgrow(bot, Priority.NEVER);
				GridPane.setVgrow(bot, Priority.NEVER);
				ui.add(bot, 0, 1, 1,1);
				
				Pane right = wworkspace.getUI();
				right.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
				right.setPrefWidth(640);
				right.setPrefHeight(640);
				GridPane.setHgrow(right, Priority.ALWAYS);
				ui.add(right, 1, 0, 1, 2);
				
				stage.setScene(scene);
				stage.show();
			}	
		});
	}
	
	static Stage cutEditorStage = null;
	
	protected static void mkCutEditor(TypeHierarchy h)
	{
		if(cutEditorStage == null)
		{
			CutEditor editor = new CutEditor(h);
			cutEditorStage = new Stage();
			Pane ui = editor.getUI();
			
			Scene cutEditorScene;
			cutEditorScene = new Scene(ui);
			
			cutEditorStage.setScene(cutEditorScene);
		}
		
		if(!cutEditorStage.isShowing())
			cutEditorStage.show();
	}

	protected static void mkFrames(WWorkArea wworkspace)
	{
		for(WBlock c : wworkspace.children)
		{
			if(c instanceof WindowBlock)
			{
				((WindowBlock)c).mkFrame();
			}
		}
	}
}
